<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace kitego\enum;

final class ClientTypeEnum
{
    private function __construct()
    {
    } // 防止实例化

    const UNKNOWN = 0; // 未知
    const ADMIN_API = 1; // 管理端
    const API = 2; // 用户端

    // 类型与路径前缀映射
    private static $pathMap = [
        self::ADMIN_API => ['/adminapi'], // 管理端路径前缀
        self::API => [] // 用户端无特定前缀
    ];

    /**
     * 获取类型对应的路径前缀
     */
    public static function getPathPrefixes($type)
    {
        return self::$pathMap[$type] ?? [];
    }

    /**
     * 根据路径判断客户端类型
     */
    public static function fromPath($path)
    {
        // 遍历所有类型的路径前缀
        foreach (self::$pathMap as $type => $prefixes) {
            foreach ($prefixes as $prefix) {
                if (strpos($path, $prefix) === 0) {
                    return $type;
                }
            }
        }

        return self::UNKNOWN;
    }

    /**
     * 验证是否为有效类型
     */
    public static function isValid($type)
    {
        return in_array($type, [self::API, self::ADMIN_API]);
    }
}
